package com.sanyang.pda.ui.manualpicking;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sanyang.pda.adapter.PickingDeliveryAdapter;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.PickingDeliveryBean;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.bean.SelectBean;
import com.sanyang.pda.bean.request.EnterToBePickedRequest;
import com.sanyang.pda.contract.PickingDeliveryContract;
import com.sanyang.pda.dialog.SelectPop;
import com.sanyang.pda.presenter.PickingDeliveryPresenter;
import com.sanyang.pda.view.ScanButView;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PickingDeliveryFragment extends BaseMvpFragment<PickingDeliveryPresenter> implements PickingDeliveryContract.View {
    @BindView(R.id.tv_tray)
    TextView tvTray;
    @BindView(R.id.scan)
    ScanButView scan;
    @BindView(R.id.tv_kuwei)
    TextView tvKuwei;
    @BindView(R.id.bt_confirm_the_shelves)
    Button btConfirmTheShelves;
    @BindView(R.id.bt_enter_the_chosen)
    Button btEnterTheChosen;
    @BindView(R.id.tv_goods)
    TextView tvGoods;
    @BindView(R.id.choose_to_complete)
    Button chooseToComplete;

    @BindView(R.id.rv_list)
    RecyclerView rvList;
    private String entryNo;
    PickingDeliveryAdapter adapter;

    public PickingDeliveryFragment() {
    }

    public PickingDeliveryFragment(String entryNo) {
        this.entryNo = entryNo;
    }

    @Override
    public PickingDeliveryPresenter getPresenter() {
        return new PickingDeliveryPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_picking_delivery;
    }

    @Override
    public void initView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter=new PickingDeliveryAdapter(R.layout.item_pickingdelivery,new ArrayList<>());
        rvList.setAdapter(adapter);
    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        scan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                switch (scanBean.getType()) {
                    case -1://商品
                        //tvGoods.setText(scanBean.getCode());
                        presenter.selectArticleInfoByBarcode(scanBean.getCode(), entryNo);
                        break;
                    case 1://库位
                        tvKuwei.setText(scanBean.getCode());
                        break;
                    case 2://作业库位
                        tvKuwei.setText(scanBean.getCode());
                        break;
                    case 4://托盘
                        tvTray.setText(scanBean.getCode());
                        break;
                }
                if (!TextUtils.isEmpty(tvGoods.getText().toString()) && !TextUtils.isEmpty(tvKuwei.getText().toString()) && !TextUtils.isEmpty(tvTray.getText().toString())) {
                    presenter.deliveryInfo(entryNo, tvTray.getText().toString(), tvKuwei.getText().toString(), myselectBean.getId());
                }
            }
        });
       // refresh();
    }

    SelectBean myselectBean;
    List<SelectBean> selectBeanList = new ArrayList<>();

    @Override
    public void setGoodsList(List<ByBarcodeBean> list) {
        if (list.size() == 0) {
            showMessage("未查询到商品信息");
            return;
        }
        if (list.size() == 1) {
           /* if (isAdd(list.get(0).getArticleId())){
                return;
            }*/
            tvGoods.setText(list.get(0).getBarcode());
            myselectBean = new SelectBean(list.get(0).getArticleName(), list.get(0).getArticleId());
            myselectBean.setOther(list.get(0).getBarcode());
            if (!TextUtils.isEmpty(tvGoods.getText().toString()) && !TextUtils.isEmpty(tvKuwei.getText().toString()) && !TextUtils.isEmpty(tvTray.getText().toString())) {
                presenter.deliveryInfo(entryNo, tvTray.getText().toString(), tvKuwei.getText().toString(), myselectBean.getId());
            }
        }
        if (list.size() > 1) {
            selectBeanList.clear();
            for (int i = 0; i < list.size(); i++) {
                ByBarcodeBean byBarcodeBean = list.get(i);
                SelectBean selectBean = new SelectBean(byBarcodeBean.getArticleName(), byBarcodeBean.getArticleId());
                selectBean.setOther(byBarcodeBean.getBarcode());
                selectBeanList.add(selectBean);
            }
            SelectPop selectPop = new SelectPop(mContext, new SelectPop.Onclick() {
                @Override
                public void onClick(SelectBean content) {
                   /* if (isAdd(content.getId())){
                        return;
                    }*/
                    myselectBean = content;
                    tvGoods.setText(content.getOther());
                    if (!TextUtils.isEmpty(tvGoods.getText().toString()) && !TextUtils.isEmpty(tvKuwei.getText().toString()) && !TextUtils.isEmpty(tvTray.getText().toString())) {
                        presenter.deliveryInfo(entryNo, tvTray.getText().toString(), tvKuwei.getText().toString(), myselectBean.getId());
                    }
                }
            });
            selectPop.show(selectBeanList);
        }
    }
    public boolean isAdd(String id){
        boolean is=false;
        for (PickingDeliveryBean pickingDeliveryBean : adapter.getData()){
            if (id.equals(pickingDeliveryBean.getArticleId())){
                is=true;
            }
        }
        if (is){
            showMessage(getString(R.string.gspjcz));
        }
        return is;
    }
    @Override
    public void onPause() {
        super.onPause();
        scan.setScanCallBack(null);
    }

    @OnClick(R.id.choose_to_complete)
    public void onComfireClicked() {
        EnterToBePickedRequest enterToBePickedRequest = new EnterToBePickedRequest();
        enterToBePickedRequest.setDeliveryNo(entryNo);
        enterToBePickedRequest.setPallet(tvTray.getText().toString());
        enterToBePickedRequest.setCurrentStoragebin(tvKuwei.getText().toString());
        presenter.pickingCompleted(enterToBePickedRequest);
    }

    @OnClick(R.id.bt_confirm_the_shelves)
    public void onBtConfirmTheShelvesClicked() {
        if (TextUtils.isEmpty(tvTray.getText().toString())) {
            showMessage(getString(R.string.tray) + getString(R.string.not_be_empty));
            return;
        }
        List<String> list=new ArrayList<>();
        for (PickingDeliveryBean item: adapter.getData()){
            list.add(item.getArticleId());
        }
        presenter.confirmOffShelf(entryNo, tvTray.getText().toString(), tvKuwei.getText().toString(), list);
    }

    @OnClick(R.id.bt_enter_the_chosen)
    public void onBtEnterTheChosenClicked() {
        if (TextUtils.isEmpty(tvTray.getText().toString())) {
            showMessage(getString(R.string.tray) + getString(R.string.not_be_empty));
            return;
        }
        EnterToBePickedRequest enterToBePickedRequest = new EnterToBePickedRequest();
        enterToBePickedRequest.setDeliveryNo(entryNo);
        enterToBePickedRequest.setPallet(tvTray.getText().toString());
        enterToBePickedRequest.setCurrentStoragebin(tvKuwei.getText().toString());
        presenter.enterToBePicked(enterToBePickedRequest);
    }

    @Override
    public void setList(PickingDeliveryBean pickingDeliveryBean) {
        if (isAdd(pickingDeliveryBean.getArticleId())){
            return;
        }
        adapter.addData(pickingDeliveryBean);
        if (TextUtils.isEmpty(pickingDeliveryBean.getState())) {
            btConfirmTheShelves.setVisibility(View.VISIBLE);
            btEnterTheChosen.setVisibility(View.INVISIBLE);
            chooseToComplete.setVisibility(View.INVISIBLE);
        }

        if (TextUtils.equals(pickingDeliveryBean.getState(), getString(R.string.yixiajia))) {
           // tvKuwei.setText("");
            btEnterTheChosen.setVisibility(View.VISIBLE);
            btConfirmTheShelves.setVisibility(View.INVISIBLE);
            chooseToComplete.setVisibility(View.INVISIBLE);
        }
        if (TextUtils.equals(pickingDeliveryBean.getState(), getString(R.string.daijianxuan))) {
            btEnterTheChosen.setVisibility(View.INVISIBLE);
            btConfirmTheShelves.setVisibility(View.INVISIBLE);
            chooseToComplete.setVisibility(View.VISIBLE);
        }
        if (TextUtils.equals(pickingDeliveryBean.getState(), getString(R.string.yijianxuan))) {
            btEnterTheChosen.setVisibility(View.INVISIBLE);
            btConfirmTheShelves.setVisibility(View.INVISIBLE);
            chooseToComplete.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void refresh() {
        if (!TextUtils.isEmpty(tvGoods.getText().toString()) && !TextUtils.isEmpty(tvKuwei.getText().toString()) && !TextUtils.isEmpty(tvTray.getText().toString())) {
            presenter.deliveryInfo(entryNo, tvTray.getText().toString(), tvKuwei.getText().toString(), myselectBean.getId());
        }
    }

    @Override
    public void onSuccess() {
        adapter.setNewInstance(new ArrayList<>());
        tvKuwei.setText("");
        tvGoods.setText("");
        tvTray.setText("");
        btConfirmTheShelves.setVisibility(View.INVISIBLE);
        btEnterTheChosen.setVisibility(View.INVISIBLE);
        chooseToComplete.setVisibility(View.INVISIBLE);
    }

}
